package com.amarsoft.apot.action;

import com.amarsoft.apot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;


    @Autowired
    private UserService userService;


    @Resource(name="dataSyncExecutor")
    private ExecutorService coreMigrationExecutor;

    @GetMapping("/hello3")
    public String test03() {

        //事务操作类
        TransactionStatus transactionStatus = null;
        try {
//            //开启事务
//            transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
//
//            System.out.println("1111111111111111111111111"+transactionStatus);
//
//            Thread.sleep(1000);

            for(int i = 0;i<=9;i++) {
                coreMigrationExecutor.execute(() -> {
                    userService.test02(null);
                });
            }

//            //提交事务
//            dataSourceTransactionManager.commit(transactionStatus);

        } catch (Exception e) {

        }
        return "hello world1";
    }

    @PostMapping("/hello4")
    public String test04() {
        return "hello world2";
    }
}
